import http from "./axios";
import { global } from '@/config/config'
import { getValue } from "@/utils/deviceStorage";

function get_limit(url, from, to, data) {
    return new Promise((resolve, reject) => {
        let payload = {
            jsonrpc: "2.0",
            method: "eth_estimateGas",
            params: [{
                from: from,
                to: to,
                data: data

            }],
            id: 1,
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_maxPriorityFeePerGas(url) {
    return new Promise((resolve, reject) => {
        let payload = {
            jsonrpc: "2.0",
            method: "eth_maxPriorityFeePerGas",
            params: [],
            id: 1,
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_price(url) {
    return new Promise((resolve, reject) => {
        let payload = {
            jsonrpc: "2.0",
            method: "eth_gasPrice",
            params: [],
            id: 1,
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_blockinfo(url) {
    return new Promise((resolve, reject) => {
        let payload = {
            jsonrpc: "2.0",
            method: "eth_blockNumber",
            params: [],
            id: 1,
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_newblockinfo(url, block) {
    return new Promise((resolve, reject) => {
        let payload = {
            jsonrpc: "2.0",
            method: "eth_getBlockByNumber",
            params: [block, true],
            id: 1,
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function eth_getTransactionCount(url, address) {
    return new Promise((resolve, reject) => {
        let payload = {
            jsonrpc: "2.0",
            method: "eth_getTransactionCount",
            params: [address, "latest"],
            id: 0,
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function words_register(param) {
    return new Promise((resolve, reject) => {
        let url = global.BaseURL + 'wuser/words_register/'
        let payload = {
            words_string: param
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function check_address(param) {
    return new Promise((resolve, reject) => {
        let RPCNode = getValue('RPCNode')
        let url = RPCNode || global.RPCNode
        let payload = {
            jsonrpc: "2.0",
            method: "eth_getCode",
            params: [param, "latest"],
            id: 1,
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function eth_single_create(publickey, wallet_name) {
    return new Promise((resolve, reject) => {
        let half = (publickey.length - 4) / 2
        let pkey = publickey.substring(4, 4 + half)
        if (parseInt("0x" + publickey.substring(publickey.length - 1, publickey.length)) % 2 === 0) {
            pkey = "02" + pkey
        } else {
            pkey = "03" + pkey
        }
        let token = getValue('token')
        let url = global.ethapi + 'create/'
        let payload = JSON.stringify({
            "pubkey": pkey,
            "wallet_name": wallet_name
        })
        let config = {
            headers: {
                "Content-Type": 'application/json',
                "Authorization": "Bearer " + token,
            }
        }
        http.post(url, payload, config).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function wallet_list() {
    return new Promise((resolve, reject) => {
        let token = getValue('token')
        let url = global.ethapi + 'wallet_list/?page=1'
        let config = {
            headers: {
                "Authorization": "Bearer " + token,
            }
        }
        http.get(url, undefined, config).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function eth_transfer(param) {
    return new Promise((resolve, reject) => {
        let RPCNode = getValue('RPCNode')
        let url = RPCNode || global.RPCNode
        let payload = {
            "jsonrpc": "2.0",
            "method": "eth_sendRawTransaction",
            "params": [param],
            "id": 1
        }
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_transferHistory(url, address, startTime, endTime, size) {
    return new Promise((resolve, reject) => {
        let token = getValue('token')
        let payload = {
            "address": address,
            "start_time": startTime,
            "end_time": endTime,
            "size": size
        }
        let config = {
            headers: {
                "Authorization": "Bearer " + token,
            }
        }
        http.get(url, payload, config).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_safe_details_by_address(address) {
    return new Promise((resolve, reject) => {
        let chainId = getValue('chainId')
        let url = global.BaseURL1 + `v1/chains/${chainId}/safes/${address}`
        http.get(url).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_safe_balance_eth(address) {
    return new Promise((resolve, reject) => {
        let chainId = getValue('chainId')
        let url = global.BaseURL1 + `v1/chains/${chainId}/safes/${address}/balances/USD`
        let payload = {
            "exclude_spam": true,
            "trusted": false
        }
        http.get(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_safe_balance_erc721(address) {
    return new Promise((resolve, reject) => {
        let chainId = getValue('chainId')
        let url = global.BaseURL1 + `v1/chains/${chainId}/safes/${address}/collectibles`
        http.get(url).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_safe_queue(address) {
    return new Promise((resolve, reject) => {
        let chainId = getValue('chainId')
        let url = global.BaseURL1 + `v1/chains/${chainId}/safes/${address}/transactions/queued`
        http.get(url).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_safe_history(address) {
    return new Promise((resolve, reject) => {
        let chainId = getValue('chainId')
        let url = global.BaseURL1 + `v1/chains/${chainId}/safes/${address}/transactions/history`
        let payload = {
            "cursor": 'limit=0',
            "offset": 0,
            "timezone_offset": 0
        }
        http.get(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function submit_safe_transactions(address, payload) {
    return new Promise((resolve, reject) => {
        let chainId = getValue('chainId')
        let url = global.BaseURL1 + `v1/chains/${chainId}/transactions/${address}/propose`
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_safe_nonce(address, payload) {
    return new Promise((resolve, reject) => {
        let chainId = getValue('chainId')
        let url = global.BaseURL1 + `v1/chains/${chainId}/safes/${address}/multisig-transactions/estimations`
        http.post(url, payload).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_chain_detail(chainId) {
    return new Promise((resolve, reject) => {
        let url = global.BaseURL1 + `v1/chains/${chainId}`
        http.get(url).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function get_transaction_detail(detailId) {
    return new Promise((resolve, reject) => {
        let chainId = getValue('chainId')
        let url = global.BaseURL1 + `v1/chains/${chainId}/transactions/${detailId}`
        http.get(url).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

function axios_get(url) {
    return new Promise((resolve, reject) => {
        http.get(url).then(res => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}

export default {
    get_limit,
    get_maxPriorityFeePerGas,
    get_price,
    get_blockinfo,
    get_newblockinfo,
    eth_getTransactionCount,
    words_register,
    check_address,
    eth_single_create,
    wallet_list,
    eth_transfer,
    get_transferHistory,
    get_safe_details_by_address,
    get_safe_balance_eth,
    get_safe_balance_erc721,
    get_safe_queue,
    get_safe_history,
    submit_safe_transactions,
    get_safe_nonce,
    get_chain_detail,
    get_transaction_detail,
    axios_get
}
